// API - 系统部门

import { http } from "@/utils/http";
import { errorReject } from "@/utils/truncate";
import { message } from "@/utils/message";

interface ListResult extends HttpResponse {
  data?: SysDeptInfo[];
}

interface DetailResult extends HttpResponse {
  data?: SysDeptInfo;
}

export type SysDeptInfo = {
  id: Id;
  parentId: Id;
  deptName: string;
  principal: string;
  phone: string;
  email: string;
  remark: string;
  sort: string | number;
  status: boolean;
  createTime: string;
  updateTime: string;
};

/**
 * 获取列表
 *
 * @param params 请求参数
 * @param fromCache 是否从本地缓存种读取
 * @returns
 */
export const getSysDeptList = (params?: object) => {
  return errorReject(
    http.request<ListResult>("get", "/system/depts", {
      params
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 获取信息
 *
 * @param id 请求ID
 * @param params 请求参数
 * @returns
 */
export const getSysDeptDetail = (id: Id, params?: object) => {
  return errorReject(
    http.request<DetailResult>("get", "/system/depts/" + id, {
      params
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 创建信息
 *
 * @param data 请求参数
 * @returns
 */
export const addSysDeptDetail = (data?: object) => {
  return errorReject(
    http.request<DetailResult>("post", "/system/depts", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 更新信息
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const editSysDeptDetail = (id: Id, data?: object) => {
  return errorReject(
    http.request<DetailResult>("put", "/system/depts/" + id, {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 删除信息
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const delSysDeptDetail = (id: Id, data?: object) => {
  return errorReject(
    http.request<HttpResponse>("delete", "/system/depts/" + id, {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};

/**
 * 批量删除信息
 *
 * @param id 请求ID
 * @param data 请求参数
 * @returns
 */
export const batchDelSysDeptDetail = (data?: object) => {
  return errorReject(
    http.request<HttpResponse>("delete", "/system/depts", {
      data
    }),
    error => {
      message(error.message, { type: "error" });
    }
  );
};
